package com.xuqm.sdhbwfu.app.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.xuqm.base.di.manager.HttpManager
import com.xuqm.base.dialog.loading.LoadingDialog
import com.xuqm.base.extensions.loge
import com.xuqm.base.extensions.showMessage
import com.xuqm.sdhbwfu.app.model.JobListModel
import com.xuqm.sdhbwfu.app.model.UploadModel
import com.xuqm.sdhbwfu.app.repository.Service
import com.xuqm.sdhbwfu.core.extensions.io_main
import com.xuqm.sdhbwfu.core.extensions.subscribeBy
import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ProcessingDetailViewModel : BaseViewModel() {


    private val _status = MutableLiveData<UploadModel>()
    val status: LiveData<UploadModel> = _status
    fun picUpload(logFile: File) {
        val fileBody: RequestBody =
            RequestBody.create(MediaType.parse("application/octet-stream"), logFile)
        val body = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", "", fileBody)
            .build()

        HttpManager.getApi(Service::class.java)
            .picUpload(body)
            .io_main()
            .subscribeBy(
                {
                    _status.postValue(it)
                }, {
                    _status.postValue(null)
                    showMessage("上传图片失败::$it")
                }
            ).adds()
    }

    private val _up = MutableLiveData<JobListModel>()
    val up: LiveData<JobListModel> = _up
    fun inspectionUpdate(mId:Int,model: JobListModel) {
        HttpManager.getApi(Service::class.java)
            .inspectionUpdate(mId, model)
            .io_main()
            .subscribeBy(
                {
                    it?.loge()
                    _up.postValue(it)
                }, {
                    LoadingDialog.dismissDialog()
                    _up.postValue(null)
                    it.loge()
                    showMessage(it)
                }
            ).adds()
    }
}